package risoe.syslab.fhnew.gui;

/*
 * Copyright (c) 2012-2014, Technical University of Denmark (DTU)
 * All rights reserved.
 * 
 * The Flexhouse 2.0 platform ("this software") is licensed under the
 * BSD 3-clause license.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   - Neither the name of DTU nor the names of its contributors may be used to
 *     endorse or promote products derived from this software without specific
 *     prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE TECHNICAL UNIVERSITY OF DENMARK BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import java.io.Serializable;
import java.util.ArrayList;

/**
 * Used for passing update requests from controllers to related Visualizations
 * @author M. Svendsen
 */
public class VisualizationUpdate implements Serializable{
	private static final long serialVersionUID = -719333353854213778L;

	/**
	 * Container class used for indicating whether a command relates to text or the setting of an actual attribute
	 */
	public final class CommandHolder implements Serializable{
		private static final long serialVersionUID = 8290760692460321025L;
		private final String mId;
		private final String mAttributeName;
		private final String mCommand;
		
		/**
		 * Constructs a new CommandHolder using the supplied arguments
		 * @param id the id of the element whose attributes should be changed
		 * @param attributeName the name of the attribute to change
		 * @param command the new value that should be attached to the attribute
		 */
		public CommandHolder(String id, String attributeName, String command) {
			mId = id;
			mAttributeName = attributeName;
			mCommand = command;
		}
		
		public String getId(){
			return mId;
		}
		
		public String getAttributeName(){
			return mAttributeName;
		}
		
		public String getCommand(){
			return mCommand;
		}
	}
	
	private final String mDestination;
	private final ArrayList<CommandHolder> mCommands = new ArrayList<CommandHolder>();
	
	/**
	 * Constructs a new VisualizationUpdate for the visualization identified by the name parameter
	 * @param name a string representing the name of the visualization this event should be sent to.
	 * NOTE: The name should be the same as the one used for the controller
	 */
	public VisualizationUpdate(String name){
		mDestination = name;
	}
	
	/**
	 * @return the destination of this VisualizationUpdate(The name of the Visualization that spawned this event)
	 */
	public String getDestination(){
		return mDestination;
	}
	
	/**
	 * @return an ArrayList of CommandHolder objects representing all the updates that should be carried out
	 * by the UIManager as a result of this VisualizationUpdate.
	 */
	public ArrayList<CommandHolder> getCommands(){
		return mCommands;
	}

	/**
	 * Adds a command to this VisualizationUpdate
	 * @param id the id of the element that should have an attribute changed
	 * @param attributeName the name of the attribute to change. If the attribute is a nested attribute, the
	 * attributeName parameter should be specified as "outer:inner"
	 * @param command the new value that the attribute should be set to
	 * @return a reference to this VisualizationUpdate, allowing chaining of calls
	 */
	public VisualizationUpdate addCommand(String id, String attributeName, String command){
		mCommands.add(new CommandHolder(id, attributeName, command));
		return this;
	}
	
	/**
	 * Adds a command referring to a text node
	 * @param id the id of the text element in the visualization
	 * @param text the new text to set this element to
	 * @return a reference to this VisualizationUpdate, allowing chaining of calls
	 */
	public VisualizationUpdate addCommand(String id, String text){
		mCommands.add(new CommandHolder(id, "", text));
		return this;
	}
}

